<?php

class EditorController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    /**
     * Отображение секции
     */
    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new EditorModel();
        $content = $model->getContent($sid)->content;
        $model = new PageModel();
        $this->view->page_content = $model->replaceLinks($content);

        //Определяем снип
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $model = new SnippetModel();
        $snip = $model->getSnip($sid);
        if (isset($snip->id)) {
            $this->view->setScriptPath(APP_PATH . '/templates/default/scripts');
            $this->_helper->viewRenderer('snippet/snip', '', true);
            $this->view->snip_code = $snip->code;
        }
    }

    public function modifyAction() {
        $model = new EditorModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $content = $model->getContent($sid);
        $this->view->page_content = $content;
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            $content = array('content' => $formData['content']);
            $model->updateContent($content, $sid);
            $this->_redirect('/pages/modify/id/' . $sid);
        }
    }

    public function addAction() {
        $model = new EditorModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $model->addContent(array('sid' => $sid, 'content' => ''));
        $this->_redirect('/pages/section/id/' . $pid);
    }

}
